﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Quartz.WebManager.Model;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Quartz.WebManager.Web.Models
{
    /// <summary>
    /// Specifies that a data field value should be lower or equal to an other data field value.
    /// </summary>
    public class LowerOrEqualToClientValidator : DataAnnotationsModelValidator<LowerOrEqualToAttribute>
    {
        public const string ValidationType = "lowerorequal";

        /// <summary>
        /// Initializes a new instance of the HigherOrEqualToAttribute class.
        /// </summary>
        public LowerOrEqualToClientValidator(ModelMetadata metadata, ControllerContext context, LowerOrEqualToAttribute attribute)
            : base(metadata, context, attribute)
        {
        }

        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule()
            {
                ValidationType = LowerOrEqualToClientValidator.ValidationType,
                ErrorMessage = base.ErrorMessage,
            };

            rule.ValidationParameters.Add("linked", base.Attribute.LinkedProperty);
            yield return rule;
        }
    }
}
